//
//  JYPhotoBrowserCell.swift
//  WeiBoDemo
//
//  Created by 万佳阳 on 16/8/8.
//  Copyright © 2016年 JYWan. All rights reserved.
//

import UIKit
import SDWebImage

protocol JYPhotoBrowserCellDelegate : NSObjectProtocol {
    func imageViewClick()
}

class JYPhotoBrowserCell: UICollectionViewCell {
    // MARK:- 定义属性
    var picURL : NSURL? {
        didSet {
            setupContent(picURL)
        }
    }
    var delegate : JYPhotoBrowserCellDelegate?
    
    // MARK:- 懒加载属性
    private lazy var scrollView : UIScrollView = UIScrollView()
    private lazy var progressView : JYProgressView = JYProgressView()
    lazy var imageView : UIImageView = UIImageView()
    
    // MARK:- 构造函数
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}

// MARK:- 设置UI界面
extension JYPhotoBrowserCell {
    private func setupUI() {
        //添加子控件
        contentView.addSubview(scrollView)
        contentView.addSubview(progressView)
        scrollView.addSubview(imageView)
        
        //设置frame
        scrollView.frame = contentView.bounds
        scrollView.frame.size.width -= 20
        progressView.bounds = CGRect(x: 0, y: 0, width: 50, height: 50)
        progressView.center = CGPoint(x: contentView.bounds.width * 0.5, y: contentView.bounds.height * 0.5)
        
        //设置progressView属性
        progressView.hidden = true
        progressView.backgroundColor = UIColor.clearColor()
        
        //给imageview添加点击手势
        let tapGest = UITapGestureRecognizer(target: self, action: #selector(JYPhotoBrowserCell.imageViewClick))
        imageView.addGestureRecognizer(tapGest)
        imageView.userInteractionEnabled = true
    }
}

// MARK:- 事件监听
extension JYPhotoBrowserCell {
    @objc private func imageViewClick() {
        
        delegate?.imageViewClick()
    }
}

// MARK:- 设置大图和下载进度
extension JYPhotoBrowserCell {
    private func setupContent(picURL : NSURL?) {
        guard let picURL = picURL else {
            return
        }
        
        //计算imageView的frame
        let image = SDWebImageManager.sharedManager().imageCache.imageFromDiskCacheForKey(picURL.absoluteString)
        let width = scrollView.bounds.width
        let height = width / image.size.width * image.size.height
        var y : CGFloat = 0
        if height < scrollView.bounds.height {
            y = (scrollView.bounds.height - height) * 0.5
        }
        imageView.frame = CGRect(x: 0, y: y, width: width, height: height)
        
        //给imageView添加图片
        progressView.hidden = false
        imageView.sd_setImageWithURL(getBigPicURL(picURL), placeholderImage: image, options: [], progress: { (current, total) in
                self.progressView.progress = CGFloat(current) / CGFloat(total)
            }) { (_, _, _, _) in
                self.progressView.hidden = true
        }
        
        //设置scrollView的contentSize
        scrollView.contentSize = CGSize(width: 0, height: height)
    
    }
    
    private func getBigPicURL(picURL : NSURL) ->NSURL {
        let smallURLString = picURL.absoluteString
        let bigURLString = smallURLString.stringByReplacingOccurrencesOfString("thumbnail", withString: "bmiddle")
        
        return NSURL(string: bigURLString)!
    }
}
